﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Objects;
using System.Collections.Generic;
using BAL;

public partial class Admin_UCControls_KhachHang : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetKhachHang();
            //GetTinhThanh();

        }
    }


    public static List<DanhSachKhachHang> ListKhachHang = new List<DanhSachKhachHang>();


    private void GetKhachHang()
    {
        ListKhachHang = BALDanhSachKhachHang.GetAllItem();

        if (ListKhachHang.Count > 0)
        {
            GridKhachHang.DataSource = ListKhachHang;
        }
        GridKhachHang.PageIndex = 0;
        GridKhachHang.DataBind();

    }

    protected void GridKhachHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Sua")
        {

            foreach (DanhSachKhachHang Item in ListKhachHang)
            {
                if (Item.Id == Convert.ToInt16(e.CommandArgument))
                {
                    Session.Add("ItemKhachHang", Item);
                }
            }

            Response.Redirect("../Admin/KhachHang_Update.aspx");

        }

        if (e.CommandName == "Xoa")
        {
            KhachHang ItemKhachHang = new KhachHang();
            ItemKhachHang.Id = Convert.ToInt32(e.CommandArgument.ToString());
            int item;



            item = BALKhachHang.Delete(ItemKhachHang);
            GetKhachHang();
        }
    }

    protected void btnThemMoi_Click(object sender, EventArgs e)
    {
        Session.Remove("ItemKhachHang");
        Response.Redirect("../Admin/KhachHang_Update.aspx");
    }

    protected void btnTim_Click(object sender, EventArgs e)
    {
      
        ListKhachHang = BALDanhSachKhachHang.SearchCustomer(txtTenKhachHang.Text, txtCMND.Text, Convert.ToInt16(ddlTrangThaiDuyet.SelectedValue), txtNgayDangKy.Text) ;
       
        GridKhachHang.DataSource = ListKhachHang;
        GridKhachHang.PageIndex = 0;
        GridKhachHang.DataBind();

    }

    protected void GridKhachHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridKhachHang.DataSource = ListKhachHang;
        GridKhachHang.PageIndex = e.NewPageIndex;
        GridKhachHang.DataBind();
    }

    public string TrangThaiDuyet(Object trangthai)
    {
      
           string returnValue = string.Empty;
            if (!string.IsNullOrEmpty(trangthai.ToString()))
            {
                if (trangthai.ToString() == "0")
                {
                    returnValue = "Chờ duyệt";
                }
                else if (trangthai.ToString() == "1")
                {
                    returnValue = "Đã duyệt";
                }
                else { returnValue = "Không duyệt"; }
            }
            return returnValue;
        }
}
